# coding:utf-8
# writingtime: 
import numpy as np
from math import exp,log


def f(IVq_ROF, q):
    '''
    :param IVq_ROF: 广义正交数
    :param q:
     :return:
    '''
    a = IVq_ROF[0][0] ** q
    b = IVq_ROF[0][1] ** q
    c = IVq_ROF[1][0] ** q
    d = IVq_ROF[1][1] ** q
    score = ((abs(a - c) ** (1 / q) + abs(b - d) ** (1 / q)) / 2) + 1
    # Accaurate=(b-a)**(1/q)+(d-c)**(1/q)
    return round(score / 2, 4)


def getScore(a,b,c,d):

    e = 1 - b - d
    f = 1 - a - c
    k = 21
    alpha = 0.88
    beta = 0.88
    theta = 2.25
    delta = 0.61
    gamma = 0.69
    point_u = []
    point_v = []
    point_pi = []
    step_u = (b - a) / (k - 1)
    step_v = (d - c) / (k - 1)
    step_pi = (f - e) / (k - 1)
    for i in range(k):
        point_u.append(a + step_u * i)
        point_v.append(c + step_v * i)
        point_pi.append(e + step_pi * i)
    average = 0.5
    p = 1 / k
    possibilty = []
    for i in range(len(point_u)):
        pos = (1 / ((2 * np.pi) ** 0.5)) * np.exp(-1 * ((abs(point_u[i]) - 0.5) ** 2) / 2)
        possibilty.append(pos)
    pi1 = (p ** gamma) / (p ** gamma + (1 - p) ** gamma) ** (1 / gamma)
    pi2 = (p ** delta) / (p ** delta + (1 - p) ** delta) ** (1 / delta)
    prospect_u = [0 for i in range(k)]  # 取中间值为参考点
    d_u = [0 for i in range(k)]
    for i in range(k):
        d_u[i] = point_u[i] - average
        if (d_u[i] >= 0):
            prospect_u[i] = (d_u[i] ** alpha) * pi1
        if (d_u[i] < 0):
            prospect_u[i] = -1 * theta * ((-1 * d_u[i]) ** beta) * pi2
            # 获得每个点对应前景价值
    prospect_v = [0 for i in range(k)]  # 取中间值为参考点
    d_v = [0 for i in range(k)]
    for i in range(k):
        d_v[i] = point_v[i] - average
        if (d_v[i] >= 0):
            prospect_v[i] = 1 * (d_v[i] ** alpha) * pi1
        if (d_v[i] < 0):
            prospect_v[i] = -1 * ((-1 * d_v[i]) ** beta) * theta * pi2
    prospect_pi = [0 for i in range(k)]  # 取中间值为参考点
    # print(prospect_u)
    d_pi = [0 for i in range(k)]
    for i in range(k):
        d_pi[i] = point_pi[i] - average
        if (d_pi[i] >= 0):
            prospect_pi[i] = (d_pi[i] ** alpha) * pi1
        if (d_pi[i] < 0):
            prospect_pi[i] = -1 * theta * ((-1 * d_pi[i]) ** beta) * pi2
    weight_u = []
    minum = -1 * theta * ((0.5) ** beta) * pi2
    p_u = [0 for i in range(k)]
    for i in range(k):
        p_u[i] = prospect_u[i] - minum
    for i in range(k):
        sum1 = sum(p_u)
        if (sum1 == 0):
            weight_u.append(1 / k)
        else:
            weight_u.append(p_u[i] / sum1)
    weight_v = []
    p_v = [0 for i in range(k)]
    for i in range(k):
        p_v[i] = prospect_v[i] - minum
    for i in range(k):
        sum4 = sum(p_v)
        if (sum4 == 0):
            weight_v.append(1 / k)
        else:
            weight_v.append(p_v[i] / sum(p_v))
    weight_pi = []
    p_pi = [0 for i in range(k)]
    for i in range(k):
        p_pi[i] = prospect_pi[i] - minum
    for i in range(k):
        sum4 = sum(p_pi)
        if (sum4 == 0):
            weight_pi.append(1 / k)
        else:
            weight_pi.append(p_pi[i] / sum(p_pi))
    # print(point_u, '\n', point_v, '\n', point_pi)
    # print(weight_u, '\n', weight_v, '\n', weight_pi)
    value_u = []
    value_v = []
    value_pi = []
    for i in range(k):
        value_u.append(weight_u[i] * point_u[i])
        value_v.append(weight_v[i] * point_v[i])
        value_pi.append(weight_pi[i] * point_pi[i])
    # print(value_u, '\n', value_v, '\n', value_pi)
    u = sum(value_u)
    v = sum(value_v)
    pi = sum(value_pi)
    # print(u,v)
    score1 = log((exp(2 * (u - v)) / (1 +(1-a-c+1-b-d)/2)) ** 0.5, exp(1))
    return score1

def getScore_updata(a, b, c, d):
    e = 1 - b - d
    f = 1 - a - c
    k = 21
    alpha = 0.88
    beta = 0.88
    theta = 2.25
    delta = 0.61
    gamma = 0.69
    point_u = []
    point_v = []
    point_pi = []
    step_u = (b - a) / (k - 1)
    step_v = (d - c) / (k - 1)
    step_pi = (f - e) / (k - 1)
    # for i in range(k):
    #     point_u.append(a + step_u * i)
    #     point_v.append(c + step_v * i)
    #     point_pi.append(e + step_pi * i)
    # point_u = [a + step_u * i for i in range(k)]
    # point_v = [c + step_v * i for i in range(k)]
    # point_pi = [e + step_pi * i for i in range(k)]
    average = 0.5
    # p = 1 / k
    # possibilty = []
    # for i in range(len(point_u)):
    #     pos = (1 / ((2 * np.pi) ** 0.5)) * np.exp(-1 * ((abs(point_u[i]) - 0.5) ** 2) / 2)
    #     possibilty.append(pos)
    # possibilty=[(1 / ((2 * np.pi) ** 0.5)) * np.exp(-1 * ((abs(point_u[i]) - 0.5) ** 2) / 2) for i in range(k)]
    pi1 = ((1 / k) ** gamma) / ((1 / k) ** gamma + (1 - (1 / k)) ** gamma) ** (1 / gamma)
    pi2 = ((1 / k) ** delta) / ((1 / k) ** delta + (1 - (1 / k)) ** delta) ** (1 / delta)
    prospect_u = [0 for i in range(k)]  # 取中间值为参考点
    # 写法更新部分
    '''开始'''
    point_u = np.array([a + step_u * i for i in range(k)])
    point_v = np.array([c + step_v * i for i in range(k)])
    point_pi = np.array([e + step_pi * i for i in range(k)])
    point = np.array([point_u, point_v, point_pi])
    dis = point - average
    prospect = np.array(
        [[(i ** alpha) * pi1 if i >= 0 else -1 * theta * ((-1 * i) ** beta) * pi2 for i in j] for j in dis])
    # print(prospect)
    # print(prospect)
    p = prospect - (-1 * theta * ((0.5) ** beta) * pi2)
    weight = np.array([[1 / k for i in range(k)] if sum(i) == 0 else i / sum(i) for i in p])
    value = weight * point
    # print(value)
    '''结束'''
    '''for i in range(k):
        d_u[i] = point_u[i] - average
        if (d_u[i] >= 0):
            prospect_u[i] = (d_u[i] ** alpha) * pi1
        if (d_u[i] < 0):
            prospect_u[i] = -1 * theta * ((-1 * d_u[i]) ** beta) * pi2
            # 获得每个点对应前景价值
    prospect_v = [0 for i in range(k)]  # 取中间值为参考点
    d_v = [0 for i in range(k)]
    for i in range(k):
        d_v[i] = point_v[i] - average
        if (d_v[i] >= 0):
            prospect_v[i] = 1 * (d_v[i] ** alpha) * pi1
        if (d_v[i] < 0):
            prospect_v[i] = -1 * ((-1 * d_v[i]) ** beta) * theta * pi2
    prospect_pi = [0 for i in range(k)]  # 取中间值为参考点
    d_pi = [0 for i in range(k)]
    for i in range(k):
        d_pi[i] = point_pi[i] - average
        if (d_pi[i] >= 0):
            prospect_pi[i] = (d_pi[i] ** alpha) * pi1
        if (d_pi[i] < 0):
            prospect_pi[i] = -1 * theta * ((-1 * d_pi[i]) ** beta) * pi2

    weight_u = []
    minum = -1 * theta * ((0.5) ** beta) * pi2
    p_u = [0 for i in range(k)]
    for i in range(k):
        p_u[i] = prospect_u[i] - minum
    for i in range(k):
        sum1 = sum(p_u)
        if (sum1 == 0):
            weight_u.append(1 / k)
        else:
            weight_u.append(p_u[i] / sum1)
    weight_v = []
    p_v = [0 for i in range(k)]
    for i in range(k):
        p_v[i] = prospect_v[i] - minum
    for i in range(k):
        sum4 = sum(p_v)
        if (sum4 == 0):
            weight_v.append(1 / k)
        else:
            weight_v.append(p_v[i] / sum(p_v))
    weight_pi = []
    p_pi = [0 for i in range(k)]
    for i in range(k):
        p_pi[i] = prospect_pi[i] - minum
    for i in range(k):
        sum4 = sum(p_pi)
        if (sum4 == 0):
            weight_pi.append(1 / k)
        else:
            weight_pi.append(p_pi[i] / sum(p_pi))

    value_u = []
    value_v = []
    value_pi = []
    for i in range(k):
        value_u.append(weight_u[i] * point_u[i])
        value_v.append(weight_v[i] * point_v[i])
        value_pi.append(weight_pi[i] * point_pi[i])'''
    u = sum(value[0])
    v = sum(value[1])
    pi = sum(value[2])
    score1 = log((exp(2 * (u - v)) / (1 + (1 - a - c + 1 - b - d) / 2)) ** 0.5, exp(1))
    return score1

def getScore_new(a, b, c, d):
    e = 1 - b - d
    f = 1 - a - c
    k = 21
    alpha = 0.88
    beta = 0.88
    theta = 2.25
    delta = 0.61
    gamma = 0.69
    step_u = (b - a) / (k - 1)
    step_v = (d - c) / (k - 1)
    step_pi = (f - e) / (k - 1)
    average = 0.5
    pi1 = ((1 / k) ** gamma) / ((1 / k) ** gamma + (1 - (1 / k)) ** gamma) ** (1 / gamma)
    pi2 = ((1 / k) ** delta) / ((1 / k) ** delta + (1 - (1 / k)) ** delta) ** (1 / delta)
    point_u = np.array([a + step_u * i for i in range(k)])
    point_v = np.array([c + step_v * i for i in range(k)])
    point_pi = np.array([e + step_pi * i for i in range(k)])
    point = np.array([point_u, point_v, point_pi])
    dis = point - average
    prospect = np.array(
        [[(i ** alpha) * pi1 if i >= 0 else -1 * theta * ((-1 * i) ** beta) * pi2 for i in j] for j in dis])
    p = prospect - (-1 * theta * ((0.5) ** beta) * pi2)
    weight = np.array([[1 / k for i in range(k)] if sum(i) == 0 else i / sum(i) for i in p])
    value = weight * point
    u = sum(value[0])
    v = sum(value[1])
    pi = sum(value[2])
    score1 = log((exp(2 * (u - v)) / (1 + (1 - a - c + 1 - b - d) / 2)) ** 0.5, exp(1))
    return score1


if __name__ == "__main__":
    print(getScore(0.0, 0.0, 0.0, 0.0))
